package com.luxsoft.sw3.cfd.crypto;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

public class VerSig2 {
	
	public static void main(String[] args) {
		/* Verify a DSA signature */

        try{
        	FileInputStream keyfis=new FileInputStream("C:\\sw3\\cfd\\suepk2.key");
        	byte[] encKey = new byte[keyfis.available()];  
            keyfis.read(encKey);
            keyfis.close();
            
            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
            KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
            PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
            
            FileInputStream sigfis = new FileInputStream("C:\\sw3\\cfd\\sig2");
            byte[] sigToVerify = new byte[sigfis.available()]; 
            sigfis.read(sigToVerify);
            sigfis.close();
            
            Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
            sig.initVerify(pubKey);
            
            ByteArrayInputStream datafis = new ByteArrayInputStream(GenSig2.CADENA_ORIGINAL.getBytes());
            BufferedInputStream bufin = new BufferedInputStream(datafis);

            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                sig.update(buffer, 0, len);
            };

            bufin.close();
            
            boolean verifies = sig.verify(sigToVerify);
            System.out.println("signature verifies: " + verifies);
            
        // the rest of the code goes here

        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }

	}

}
